package core;

/**
 * An attribute that represents a finite range of named integer values.
 */
public class EnumAttribute extends NumericAttribute
{
	public EnumAttribute() {
		this(null);
	}

	/**
	 * Create a new EnumAttribute with the given identifier
	 * @param name a String identifier
	 */
	public EnumAttribute(String name) {
		super(name, null);
	}

	/**
	 * Add the named value to this enumeration
	 * @param value a String value
	 */
	public void addLabel(String value) {
		labels.add(value);
	}

	public java.util.List<String> getLabels() {
		return java.util.Collections.unmodifiableList(labels);
	}

	public void setLabels(java.util.List<String> labels) {
		this.labels.clear();
		this.labels.addAll(labels);
	}

	/**
	 * Sets the current value of this <code>EnumAttribute</code> to (<code>value</code> % x),
	 * where 'x' is the number of enum values this <code>EnumAttribute</code> represents.
	 * If x is zero, this method does nothing.
	 * @param value An integer representing one of this <code>EnumAttribute</code>'s values.
	 */
	public void setValue(IntegerValue value) {
		if (value == null)
			super.setValue(null);
		else if (labels.size() > 0)
			super.setValue(new IntegerValue(value.getValue() % labels.size()));
	}

	public String toString() {
		return getName() + " = " + (getValue() == null ? "undefined" : labels.get(getValue().getValue()));
	}

	private final java.util.List<String> labels = new java.util.ArrayList<String>();
}